package com.daxia.ogmall.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.daxia.ogmall.common.DesignType;
import com.daxia.ogmall.dao.DesignDAO;
import com.daxia.ogmall.dto.DesignDTO;
import com.daxia.ogmall.dto.HouseTypeDTO;
import com.daxia.ogmall.model.CommunityHouseType;
import com.daxia.ogmall.model.Design;
import com.daxia.ogmall.model.HouseType;
import com.daxia.ogmall.model.User;
import com.daxia.ogmall.support.Page;
import com.daxia.ogmall.util.BeanMapper;

@Service
public class DesignService {
	
	@Autowired
	private DesignDAO designDAO;
	@Autowired
	private HouseTypeService houseTypeService;
	
	public List<DesignDTO> find(DesignDTO query, Page page) {
		List<Design> models = designDAO.find(query, page); 
		List<DesignDTO> dtos = toDTOs(models);
		return dtos;
	}
	
	public List<DesignDTO> findAll() {
		return this.find(new DesignDTO(), null);
	}
	
	public List<DesignDTO> find(int count) {
		Page page = new Page();
		page.setPage(1);
		page.setPageSize(count);
		return this.find(new DesignDTO(), page);
	}
	
	public Long create(DesignDTO dto) {
		Design model = new Design();
		toModel(model, dto);
		return designDAO.create(model);
	}
	
	public DesignDTO load(Long id) {
	    Design model = designDAO.load(id);
	    return toDTO(model);
    }

	public void updateAllFields(DesignDTO dto) {
		Design model = designDAO.load(dto.getId());
		toModel(model, dto);
		designDAO.update(model);
    }

	public void deleteByIds(Long[] ids) {
		if (ids != null) {
			for (Long id : ids) {
				designDAO.deleteById(id);
			}
		}
    }
	
	public void deleteById(Long id) {
		this.deleteByIds(new Long[] {id});
	}

	public DesignDTO findOne(DesignDTO query) {
		Design model = designDAO.findOne(query);
		return toDTO(model);
	}
	
	private List<DesignDTO> toDTOs(List<Design> models) {
		if (CollectionUtils.isEmpty(models)) {
			return new ArrayList<DesignDTO>(0);
		}
		List<DesignDTO> dtos = new ArrayList<DesignDTO>(models.size());
		for (Design model : models) {
	        DesignDTO dto = toDTO(model);
	        dtos.add(dto);
        }
	    return dtos;
    }
	
	protected List<DesignDTO> toDesignDTOs(List<Design> models) {
		if (CollectionUtils.isEmpty(models)) {
			return new ArrayList<DesignDTO>(0);
		}
		List<DesignDTO> dtos = new ArrayList<DesignDTO>(models.size());
		for (Design model : models) {
	        DesignDTO dto = toDTO(model);
	        dtos.add(dto);
        }
	    return dtos;
    }
    
    private DesignDTO toDTO(Design model) {
		if (model == null) {
			return null;
		}
		DesignDTO dto = BeanMapper.map(model, DesignDTO.class);
		
		return dto;
	}
	
	private void toModel(Design model, DesignDTO dto) {
		BeanMapper.copy(dto, model);
    }
	
	@SuppressWarnings("unused")
	private List<Design> toModels(List<DesignDTO> dtos) {
		if (CollectionUtils.isEmpty(dtos)) {
			return new ArrayList<Design>(0);
		}
		List<Design> models = new ArrayList<Design>(dtos.size());
		for (DesignDTO dto : dtos) {
	        Design model = new Design();
	        toModel(model, dto);
	        models.add(model);
        }
		return models;
	}

	public List<DesignDTO> findByCommunityHouseType(Long communityHouseTypeId, Page page) {
		DesignDTO query = new DesignDTO();
		query.setCommunityHouseType(new CommunityHouseType());
		query.getCommunityHouseType().setId(communityHouseTypeId);
		return this.find(query, page);
	}
	
	public Integer countByCompanyId(Long companyId) {
		
		return designDAO.countByCompanyId(companyId);
	}

	/**
	 * 查找楼盘方案
	 * @param i
	 * @return
	 */
	public List<DesignDTO> findCommunityDesign(Page page) {
		DesignDTO query = new DesignDTO();
		query.setType(DesignType.Community.getValue());
		return this.find(query, page);
	}

	public List<DesignDTO> findByUserId(Long id, Integer count) {
		DesignDTO query = new DesignDTO();
		query.setUser(new User());
		query.getUser().setId(id);
		
		Page page = new Page();
		page.setPageSize(count);
		
		return this.find(query, page);
	}
	
	public DesignDTO findByPlanId(String planId) {
		DesignDTO query = new DesignDTO();
		query.setPlanId(planId);
		query.setFromAdmin(true);
		return this.findOne(query);
	}

	public List<HouseTypeDTO> findDistinctHouseTypesByCommunity(Long id) {
		List<HouseType> houseTypes = designDAO.findDistinctHouseTypesByCommunity(id);
		
		return houseTypeService.toDTOs(houseTypes);
	}

	public List<String> findDistinctAreas(Long communityId, Long houseTypeId) {
		return designDAO.findDistinctAreas(communityId, houseTypeId);
	}
}
